package mesweb.mesweb.Utils;

import cn.hutool.core.lang.Console;
import mesweb.mesweb.entity.device.MesDeviceType;

import java.util.ArrayList;
import java.util.List;

public class TreeUtil {
    /**
     * 接口包含的参数明细
     */
    private List<MesDeviceType> MesDeviceTypeList;

    public List<MesDeviceType> getMesDeviceTypeList() {
        return MesDeviceTypeList;
    }
    public void setMesDeviceTypeList(List<MesDeviceType> MesDeviceTypeList) {
        this.MesDeviceTypeList = MesDeviceTypeList;
    }

    //建立树形结构
    public List<MesDeviceType> builTree() {
        List<MesDeviceType> treeMenus = new ArrayList();
        for (MesDeviceType menuNode : getRootNode()) {
            menuNode = buildChilTree(menuNode);
            treeMenus.add(menuNode);
        }
        return treeMenus;
    }

    //递归，建立子树形结构
    private MesDeviceType buildChilTree(MesDeviceType pNode) {
        List chilMenus = new ArrayList();

        for (MesDeviceType menuNode : this.getMesDeviceTypeList()) {
            Console.log("PNode:" + pNode.getId());
            if (menuNode.getParentId().equals(pNode.getId())) {
                Console.log( menuNode.getId());
                chilMenus.add(buildChilTree(menuNode));
            }
        }
        pNode.setChildren(chilMenus);
        return pNode;
    }

    //获取根节点
    private List<MesDeviceType> getRootNode() {
        List rootMenuLists = new ArrayList();
        if(this.getMesDeviceTypeList().size() == 0) return null;
        for (MesDeviceType menuNode : this.getMesDeviceTypeList()) {
            if (menuNode.getParentId() == 0) {
                rootMenuLists.add(menuNode);
            }
        }
        return rootMenuLists;
    }
}
